﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
//using System.Linq;
using System.Text;
//using System.Threading.Tasks;
using System.Windows.Forms;

namespace TRicheControl.WinformControl
{
    [ToolboxBitmap(typeof(CheckBox))]
    public partial class RicheCheckBox : Control
    {
        private bool mIsCheck = false;
        public bool IsCheck
        {
            get
            {
                return mIsCheck;
            }
            set
            {
                mIsCheck = value;
                this.Invalidate(mCheckBoxRect);
            }
        }

        private Rectangle mCheckBoxRect;
        private Rectangle mTextRect;
        private Size mCheckBoxSize = new Size(13, 13);
        public Size CheckBoxSize
        {
            get
            {
                return mCheckBoxSize;
            }
            set 
            {
                mCheckBoxSize = value;
                ResetCheckBoxRect();
                this.Refresh();
            }
        }

        private Padding mTextPadding = new Padding(5);

        public Padding TextPadding
        {
            get
            {
                return mTextPadding;
            }
            set
            {
                mTextPadding = value;
            }
        }

        public RicheCheckBox()
        {
            InitializeComponent();
            ResetCheckBoxRect();
        }

        protected override void OnPaint(PaintEventArgs pe)
        {
            DrawCheckBox(pe.Graphics);
        }

        private void ResetCheckBoxRect()
        {
            Point location = new Point(0, (this.Height - mCheckBoxSize.Height) / 2);
            Point strLocation = new Point(mCheckBoxSize.Width + mTextPadding.Left, ((this.Height - this.FontHeight) / 2) + 1);
            mCheckBoxRect = new Rectangle(location, mCheckBoxSize);
            mTextRect = new Rectangle(strLocation, new Size(this.Width - mCheckBoxSize.Width + mTextPadding.Left, this.Height));
        }

        protected override void OnResize(EventArgs e)
        {
            ResetCheckBoxRect();
            base.OnResize(e);
        }

        private void DrawCheckBox(Graphics g)
        {
            if (this.IsCheck == true)
            {
                if (this.Enabled == true)
                {
                    GraphicsStyle.Styles.CheckBoxStyle.DrawEanbelCheckBox(g, mCheckBoxRect);
                }
                else
                {
                    GraphicsStyle.Styles.CheckBoxStyle.DrawUnEanbelCheckBox(g, mCheckBoxRect);
                }
            }
            else
            {
                if (this.Enabled == true)
                {
                    GraphicsStyle.Styles.CheckBoxStyle.DrawEanbelUnCheckBox(g, mCheckBoxRect); 
                }
                else
                {
                    GraphicsStyle.Styles.CheckBoxStyle.DrawUnEanbelUnCheckBox(g, mCheckBoxRect);
                }
            }
            GraphicsStyle.Styles.CheckBoxStyle.DrawString(this.Text, this.Font, this.ForeColor, g, this.mTextRect);
        }

        protected override void OnMouseClick(MouseEventArgs e)
        {
            this.IsCheck = !IsCheck;
            base.OnMouseClick(e);
        }
    }
}
